home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / System / Mount.php < prev   
Encoding:
PHP Script  |  2005-12-02  |  6.7 KB  |  243 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Ian Eure <ieure@php.net>                                     |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Mount.php,v 1.1 2004/06/09 07:03:29 ieure Exp $
  20.  
  21. require_once 'PEAR.php';
  22. require_once 'File/Fstab.php';
  23. require_once 'System/Command.php';
  24.  
  25. /**
  26.  * Mount and unmount devices in fstab
  27.  *
  28.  * $sm = &new System_Mount();
  29.  * $cdrom = &$sm->getEntryForPath('/cdrom');
  30.  * $cdrom->mount();
  31.  * ...
  32.  * $cdrom->unMount();
  33.  * 
  34.  * @package System_Mount
  35.  * @version 1.0.0
  36.  * @author Ian Eure <ieure@php.net>
  37.  * @copyright Copyright © 2004, Ian Eure
  38.  * @license http://www.php.net/license/3_0.txt PHP License 3.0
  39.  * @link http://atomized.org/PEAR/System_Mount/
  40.  */
  41. class System_Mount extends File_Fstab {
  42.     /**
  43.      * Default options
  44.      *
  45.      * @var array
  46.      */
  47.     var $_defaultMountOptions = array(
  48.         'entryClass' => "System_Mount_Entry",
  49.         'mtabFile' => "/etc/mtab",
  50.         'mountCmd' => "mount",
  51.         'umountCmd' => "umount"
  52.     );
  53.  
  54.  
  55.     /**
  56.      * Constructor
  57.      *
  58.      * @param $options array Class options
  59.      * @see File_Fstab::setOptions()
  60.      * @return void
  61.      */
  62.     function System_Mount($options = false)
  63.     {
  64.         $pc = get_parent_class($this);
  65.  
  66.         // This is a bit ugly.
  67.         if (!$options) {
  68.             $options = array();
  69.         }
  70.         $opts = array_merge($this->_defaultMountOptions, $options);
  71.  
  72.         // Get a static mtab instance
  73.         $mtab = &PEAR::getStaticProperty('System_Mount', 'mtab');
  74.         $mtab = File_Fstab::singleton($opts['mtabFile']);
  75.  
  76.         parent::$pc($opts);
  77.  
  78.         // Make sure the entryClass knows how to mount/unmount
  79.         $options = &PEAR::getStaticProperty('System_Mount', 'options');
  80.         $options = $this->options;
  81.     }
  82. }
  83.  
  84. /**
  85.  * Class which handles the mount/unmount operation
  86.  *
  87.  * This is the heart of System_Mount- the main class exists only to instruct
  88.  * File_Fstab to use this class, making it easier on the end-user.
  89.  *
  90.  * @package System_Mount
  91.  * @version 1.0.0
  92.  * @author Ian Eure <ieure@php.net>
  93.  * @copyright Copyright © 2004, Ian Eure
  94.  * @license http://www.php.net/license/3_0.txt PHP License 3.0
  95.  */
  96. class System_Mount_Entry extends File_Fstab_Entry {
  97.     /**
  98.      * File_Fstab instance
  99.      *
  100.      * This contains an instance of File_Fstab (created with
  101.      * {@link File_Fstab::singleton()} so we don't waste memory) which parses
  102.      * /etc/mtab and holds the current device mount state.
  103.      *
  104.      * @var object
  105.      * @see File_Fstab
  106.      * @access protected
  107.      */
  108.     var $_mtab;
  109.  
  110.     /**
  111.      * System_Mount options
  112.      *
  113.      * This contains the options passed to System_Mount when it was instantiated
  114.      *
  115.      * @var array
  116.      * @see System_Mount::System_Mount()
  117.      * @access protected
  118.      */
  119.     var $_smOptions;
  120.     
  121.  
  122.     /**
  123.      * Constructor
  124.      *
  125.      * @param $entry fstab entry
  126.      * @see File_Fstab::File_Fstab()
  127.      * @return void
  128.      */
  129.     function System_Mount_Entry($entry = false)
  130.     {
  131.         $this->_smOptions = &PEAR::getStaticProperty('System_Mount', 'options');
  132.         $this->_mtab = &PEAR::getStaticProperty('System_Mount', 'mtab');
  133.  
  134.         // Thunk to parent's constructor
  135.         $pc = get_parent_class($this);
  136.         return parent::$pc($entry);
  137.     }
  138.  
  139.     /**
  140.      * Is this device mounted?
  141.      *
  142.      * @return boolean true if mounted, false if unmounted
  143.      */
  144.     function isMounted()
  145.     {
  146.         $this->_mtab->load();
  147.         PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  148.         $ent = $this->_mtab->getEntryForPath($this->mountPoint);
  149.         PEAR::popErrorHandling();
  150.  
  151.         if (PEAR::isError($ent)) {
  152.             return false;
  153.         }
  154.         
  155.         return true;
  156.     }
  157.  
  158.     /**
  159.      * May a user mount this device?
  160.      *
  161.      * @return boolean true if user may mount, false if not
  162.      */
  163.     function userMayMount()
  164.     {
  165.         return $this->hasMountOption('user');
  166.     }
  167.  
  168.  
  169.     /**
  170.      * May this device be mounted?
  171.      *
  172.      * Similar to userMayMount(), but this takes the UID the script is running as
  173.      * in to account, and returns 'true' if the current UID is 0, or the result of
  174.      * userMayMount otherwise.
  175.      *
  176.      * @see userMayMount()
  177.      * @return boolean true if user may mount, false if not
  178.      */
  179.     function mayMount()
  180.     {
  181.         // Root may mount anything
  182.         if (posix_getuid() == 0) {
  183.             return true;
  184.         }
  185.  
  186.         return $this->userMayMount();
  187.     }
  188.  
  189.     /**
  190.      * Mount the device
  191.      *
  192.      * @return mixed boolean or PEAR_Error
  193.      * @see _mount()
  194.      */
  195.     function mount()
  196.     {
  197.         if ($this->isMounted()) {
  198.             return PEAR::raiseError("{$this->mountPoint} is already mounted");
  199.         }
  200.             
  201.         return $this->_mount($this->_smOptions['mountCmd']);
  202.     }
  203.  
  204.     /**
  205.      * Unmount the device
  206.      *
  207.      * @return mixed boolean true on success, PEAR_Error otherwise
  208.      * @see _mount()
  209.      */
  210.     function unMount()
  211.     {
  212.         if (!$this->isMounted()) {
  213.             return PEAR::raiseError("{$this->mountPoint} is not mounted");
  214.         }
  215.         return $this->_mount($this->_smOptions['umountCmd']);
  216.     }
  217.  
  218.     /**
  219.      * Dispatch an (un)mount command
  220.      *
  221.      * @param int $command SYSTEM_MOUNT_CMD_MOUNT (to mount device) or _UNMOUNT
  222.      *                     (to unmount device)
  223.      * @return return value from System_Command or PEAR_Error
  224.      * @see System_Command::execute()
  225.      * @access protected
  226.      */
  227.     function _mount($command)
  228.     {
  229.         if (!$this->mayMount()) {
  230.             return PEAR::raiseError("Users may not (un)mount {$this->mountPoint}");
  231.         }
  232.  
  233.         $cmd = new System_Command;
  234.         $cmd->pushCommand($command, $this->mountPoint);
  235.         $res = $cmd->execute();
  236.  
  237.         // Update mtab
  238.         $this->_mtab->load();
  239.  
  240.         return $res;
  241.     }
  242. }
  243. ?>